import Vue from "vue";
import VueRouter from "vue-router";
import Main from "../components/Main.vue";
Vue.use(VueRouter);

const routeList = [];
function importAll(r) {
  r.keys().forEach(key => {
    routeList.push(r(key).default);
  });
}

const originalReplace = VueRouter.prototype.replace;
VueRouter.prototype.replace = function replace(location) {
  return originalReplace.call(this, location).catch(err => err);
};

const originalPush = VueRouter.prototype.push;

VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};

importAll(require.context("../router", true, /\.routes\.js/));

const routes = [
  {
    path: "/demo",
    component: () => import("../views/Demo.vue"),
    children: [
      {
        path: "/dashboard",
        name: "dashboard",
        component: () => import("../views/Dashboard.vue"),
        meta: {
          title: "统计",
          keepAlive: true
        }
      },
      {
        path: "/list/example1",
        name: "list-example1",
        component: () => import("../views/list/Example1.vue"),
        meta: {
          title: "列表案例1",
          keepAlive: true
        }
      },
      {
        path: "/list/example2",
        name: "list-example2",
        component: () => import("../views/list/Example2.vue"),
        meta: {
          title: "列表案例2",
          keepAlive: true
        }
      },
      {
        path: "list/example1/show",
        name: "show",
        component: () => import("../views/list/Show.vue"),
        meta: {
          title: "详情页面",
          keepAlive: false
        }
      },
      {
        path: "/setting/user",
        name: "setting-user",
        component: () => import("../views/setting/user/List.vue"),
        meta: {
          title: "用户列表",
          keepAlive: true
        }
      },
      {
        path: "/setting/permission",
        name: "setting-permission",
        component: () => import("../views/setting/permission/List.vue"),
        meta: {
          title: "菜单权限管理",
          keepAlive: true
        }
      },
      {
        path: "/setting/role",
        name: "setting-role",
        component: () => import("../views/setting/role/List.vue"),
        meta: {
          title: "角色管理",
          keepAlive: true
        }
      },
      {
        path: "/goods",
        name: "goods",
        component: () => import("../views/goods/List.vue"),
        meta: {
          title: "商品管理",
          keepAlive: true
        }
      },
      {
        path: "/user/list",
        name: "user-list",
        component: () => import("../views/user/List.vue"),
        meta: {
          title: "用户管理",
          keepAlive: true
        }
      }
    ]
  },
  {
    path: "/",
    name: "Main",
    component: Main,
    children: [
      {
        path: "/dashboard",
        name: "dashboard",
        component: () => import("../views/Dashboard.vue"),
        meta: {
          title: "统计",
          keepAlive: true
        }
      },
      {
        path: "/list/example1",
        name: "list-example1",
        component: () => import("../views/list/Example1.vue"),
        meta: {
          title: "列表案例1",
          keepAlive: true
        }
      },
      {
        path: "/list/example2",
        name: "list-example2",
        component: () => import("../views/list/Example2.vue"),
        meta: {
          title: "列表案例2",
          keepAlive: true
        }
      },
      {
        path: "list/example1/show",
        name: "show",
        component: () => import("../views/list/Show.vue"),
        meta: {
          title: "详情页面",
          keepAlive: false
        }
      },
      {
        path: "/setting/user",
        name: "setting-user",
        component: () => import("../views/setting/user/List.vue"),
        meta: {
          title: "用户列表",
          keepAlive: true
        }
      },
      {
        path: "/setting/permission",
        name: "setting-permission",
        component: () => import("../views/setting/permission/List.vue"),
        meta: {
          title: "菜单权限管理",
          keepAlive: true
        }
      },
      {
        path: "/setting/role",
        name: "setting-role",
        component: () => import("../views/setting/role/List.vue"),
        meta: {
          title: "角色管理",
          keepAlive: true
        }
      },
      {
        path: "/goods",
        name: "goods",
        component: () => import("../views/goods/List.vue"),
        meta: {
          title: "商品管理",
          keepAlive: true
        }
      },
      {
        path: "/user/list",
        name: "user-list",
        component: () => import("../views/user/List.vue"),
        meta: {
          title: "用户管理",
          keepAlive: true
        }
      }
    ]
  },
  {
    path: "/about",
    name: "About",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/About.vue"),
    meta: {
      title: "ok"
    }
  }
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes
});

export default router;
